package com.yanqu.road.dao.impl.activity.shuihuyezicard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardBattleCard;
import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardRoomData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/22 18:16
 */
public class ShuiHuYeZiCardRoomDataDaoImpl extends TempDataObjectDao<ShuiHuYeZiCardRoomData> {

    public static String getShuiHuYeZiCardRoomDataSql = "select * from `t_u_activity_shuihuyezicard_room_user_data` where `activityId`=? and `userId`=?";
    public ShuiHuYeZiCardRoomData getShuiHuYeZiCardRoomData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardRoomDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateShuiHuYeZiCardRoomDataSql = "update `t_u_activity_shuihuyezicard_room_user_data` set round=?,roomId=?,rankScore=?,aiModelIndex=?,npcId=?,userCardMap=?,lastRoundWinnerId=?,buffId=?,num=?,nextBuffId=?,discardList=?,recordList=?,startTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ShuiHuYeZiCardRoomData shuiHuYeZiCardRoomData) {
        shuiHuYeZiCardRoomData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getAiModelIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getNpcId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardRoomData.getUserCardMap())));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardRoomData.getLastRoundWinnerId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getBuffId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getNextBuffId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardRoomData.getDiscardList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardRoomData.getRecordList())));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardRoomData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardRoomData.getUserId()));
        if (getDb().executeNoneQuery(updateShuiHuYeZiCardRoomDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuiHuYeZiCardRoomDataSql = "insert into `t_u_activity_shuihuyezicard_room_user_data`(activityId,userId,round,roomId,rankScore,aiModelIndex,npcId,userCardMap,lastRoundWinnerId,buffId,num,nextBuffId,discardList,recordList,startTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ShuiHuYeZiCardRoomData shuiHuYeZiCardRoomData) {
        shuiHuYeZiCardRoomData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardRoomData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getAiModelIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getNpcId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardRoomData.getUserCardMap())));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardRoomData.getLastRoundWinnerId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getBuffId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardRoomData.getNextBuffId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardRoomData.getDiscardList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardRoomData.getRecordList())));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardRoomData.getStartTime()));
        if (getDb().executeNoneQuery(insertShuiHuYeZiCardRoomDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuiHuYeZiCardRoomData parseData(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardRoomData shuiHuYeZiCardRoomData = new ShuiHuYeZiCardRoomData();
        shuiHuYeZiCardRoomData.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardRoomData.setUserId(rs.getLong("userId"));
        shuiHuYeZiCardRoomData.setRound(rs.getInt("round"));
        shuiHuYeZiCardRoomData.setRoomId(rs.getInt("roomId"));
        shuiHuYeZiCardRoomData.setRankScore(rs.getInt("rankScore"));
        shuiHuYeZiCardRoomData.setAiModelIndex(rs.getInt("aiModelIndex"));
        shuiHuYeZiCardRoomData.setNpcId(rs.getInt("npcId"));
        shuiHuYeZiCardRoomData.setUserCardMap(JSON.parseObject(rs.getString("userCardMap"), new TypeReference<Map<Long, Map<Integer, ShuiHuYeZiCardBattleCard>>>() {}));
        shuiHuYeZiCardRoomData.setLastRoundWinnerId(rs.getLong("lastRoundWinnerId"));
        shuiHuYeZiCardRoomData.setBuffId(rs.getInt("buffId"));
        shuiHuYeZiCardRoomData.setNum(rs.getInt("num"));
        shuiHuYeZiCardRoomData.setNextBuffId(rs.getInt("nextBuffId"));
        shuiHuYeZiCardRoomData.setDiscardList(JSON.parseObject(rs.getString("discardList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardDiscardCard>>() {}));
        shuiHuYeZiCardRoomData.setRecordList(JSON.parseObject(rs.getString("recordList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardPlayRecord>>() {}));
        shuiHuYeZiCardRoomData.setStartTime(rs.getLong("startTime"));

        return shuiHuYeZiCardRoomData;
    }

}
